﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using System.Xml.Linq;
//
using PerceptiveMCAPI.Types.Internal;
//
namespace PerceptiveMCAPI.Types
{
   public class listMergeVarsOutput : Api_BaseOutput
   {
      public List<listMergeVarsResults> result { get; set; }
      public listMergeVarsParms inputParms { get; private set; }
      // --------------------------------------------------------
      public listMergeVarsOutput( listMergeVarsInput input )
         : base( input )
      {
         inputParms = input.parms;
         result = new List<listMergeVarsResults>();
      }
      // --------------------------------------------------- tostring
      public override string ToString()
      {
         return string.Format( "MergeVars Listed: {0}", result.Count );
      }

      #region ================================================================== format setup

      // ---- serial
      internal void Format( string api_Request, string api_Response )
      {
         this.api_Request = api_Request;
         this.api_Response = api_Response;
         // ----------------------------------
         if ( this.api_OutputType == EnumValues.OutputType.JSON )
            outputJSON();
         else if ( this.api_OutputType == EnumValues.OutputType.XML )
            outputXML();
      }
      // ---- xmlrpc
      internal void Format( x_listMergeVars[] rpcResult )
      {
         this.api_Request = EnumValues.NotSupplied;
         this.api_Response = EnumValues.NotSupplied;
         // ------------------------
         this.result = FormatResults( rpcResult );
      }

      #endregion

      #region ================================================================= Output

      // ---------------------------------------------------------- outputXML
      protected void outputXML()
      {
         XElement xml = XElement.Parse( this.api_Response );
         // error check
         Api_Error error = apiSerial.GetXmlOutputError( xml );
         if ( error != null )
         {
            this.api_ErrorMessages.Add( error );
            return;
         }
         // -------------------------------------------- get values
         try
         {
            int i;
            IEnumerable<XElement> entries = xml.Elements( "struct" );
            foreach ( XElement entry in entries )
            {
               listMergeVarsResults mv = new listMergeVarsResults();
               mv.name = entry.Element( "name" ).Value;
               mv.req = ( entry.Element( "req" ).Value.Trim() == "1" ) ? true : false;
               mv.field_type = entry.Element( "field_type" ).Value;
               mv.isPublic = ( entry.Element( "public" ).Value.Trim() == "1" ) ? true : false;
               mv.show = ( entry.Element( "show" ).Value.Trim() == "1" ) ? true : false;
               int.TryParse( entry.Element( "order" ).Value.Trim(), out i );
               mv.order = i;
               mv.defaultValue = entry.Element( "default" ).Value;
               int.TryParse( entry.Element( "size" ).Value.Trim(), out i );
               mv.size = i;
               mv.tag = entry.Element( "tag" ).Value;
               // choices
               if ( entry.Element( "choices" ) != null && entry.Element( "choices" ).HasElements )
               {
                  IEnumerable<XElement> choices = entry.Element( "choices" ).Elements( "struct" );
                  foreach ( XElement choice in choices )
                  {
                     mv.choices.Add( choice.Value );
                  }
               }
               //
               this.result.Add( mv );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- outputJSON
      protected void outputJSON()
      {
         JavaScriptSerializer jss = new JavaScriptSerializer();

         try
         {
            if ( this.api_Response.Contains( EnumValues.JSONErrorString ) )
            {
               Api_Error err = jss.Deserialize<Api_Error>( this.api_Response );
               this.api_ErrorMessages.Add( err );
            }
            else
            {
               x_listMergeVars[] jsonResult = jss.Deserialize<x_listMergeVars[]>( this.api_Response );
               this.result = FormatResults( jsonResult );
            }
         }
         catch ( Exception ex )
         {
            this.api_ErrorMessages.Add( apiException.FormatError( ex ) );
         }
      }

      // ---------------------------------------------------------- FormatResults
      private List<listMergeVarsResults> FormatResults( x_listMergeVars[] x_results )
      {
         List<listMergeVarsResults> results = new List<listMergeVarsResults>();

         bool tb; int ti;
         foreach ( x_listMergeVars imv in x_results )
         {
            listMergeVarsResults mv = new listMergeVarsResults();
            mv.name = imv.name;
            mv.req = imv.req;
            mv.field_type = imv.field_type;
            mv.isPublic = imv.@public;
            bool.TryParse( imv.show.ToString(), out tb ); mv.show = tb;
            mv.defaultValue = imv.@default;
            int.TryParse( imv.order, out ti ); mv.order = ti;
            mv.defaultValue = imv.@default;
            int.TryParse( imv.size, out ti ); mv.size = ti;
            mv.tag = imv.tag;
            if ( imv.choices != null && imv.choices.Length > 0 )
               foreach ( string item in imv.choices )
               {
                  mv.choices.Add( item );
               }

            results.Add( mv );
         }
         return results;
      }
      // ---
      #endregion
   }
}
